﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Web;

namespace Harrby.Web.Providers.XmlProviders
{
    public class XmlRoleProvider : System.Web.Security.RoleProvider
    {
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if (String.IsNullOrEmpty(config["ApplicationName"]))
	        {
        		 
	        } 
            base.Initialize(name, config);
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            string fileName = HttpContext.Current.Server.MapPath("~/App_Data/DataStorage/XmlDataStorage/aspnet_Roles.xml");
            XDocument x = XDocument.Load(fileName);
            x.Element("aspnet_Roles").Add(new XElement("Role",
                new XElement("ApplicationId", ""),
                new XElement("RoleId", Guid.NewGuid()),
                new XElement("RoleName", roleName),
                new XElement("LoweredRoleName", roleName.ToLower()),
                new XElement("Description", "")));
            x.Save(fileName);

        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            XDocument x = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/DataStorage/XmlDataStorage/aspnet_Roles.xml"));
            return x.Element("aspnet_Roles").Elements().Select(p => p.Attribute("RoleName").Value).ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            XDocument x = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/DataStorage/XmlDataStorage/aspnet_Roles.xml"));
            return x.Element("aspnet_Roles").Elements().Where(p => p.Attribute("LoweredRoleName").Value == roleName.ToLower()).Count() > 0;
        }
    }
}
